package com.ruoyi.airsystem.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.airsystem.domain.AirFormaldehyde;
import com.ruoyi.airsystem.domain.AirWarnings;
import com.ruoyi.airsystem.mapper.AirWarningsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.airsystem.mapper.AirPmlevelsMapper;
import com.ruoyi.airsystem.domain.AirPmlevels;
import com.ruoyi.airsystem.service.IAirPmlevelsService;

/**
 * PM浓度记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-18
 */
@Service
public class AirPmlevelsServiceImpl implements IAirPmlevelsService {
    @Autowired
    private AirPmlevelsMapper airPmlevelsMapper;
    @Autowired
    private AirWarningsMapper airWarningsMapper;

    /**
     * 查询PM浓度记录
     *
     * @param recordId PM浓度记录主键
     * @return PM浓度记录
     */
    @Override
    public AirPmlevels selectAirPmlevelsByRecordId(Long recordId) {
        return airPmlevelsMapper.selectAirPmlevelsByRecordId(recordId);
    }

    /**
     * 查询PM浓度记录列表
     *
     * @param airPmlevels PM浓度记录
     * @return PM浓度记录
     */
    @Override
    public List<AirPmlevels> selectAirPmlevelsList(AirPmlevels airPmlevels) {
        return airPmlevelsMapper.selectAirPmlevelsList(airPmlevels);
    }

    /**
     * 新增PM浓度记录
     *
     * @param airPmlevels PM浓度记录
     * @return 结果
     */
    @Override
    public int insertAirPmlevels(AirPmlevels airPmlevels) {
        return airPmlevelsMapper.insertAirPmlevels(airPmlevels);
    }

    /**
     * 修改PM浓度记录
     *
     * @param airPmlevels PM浓度记录
     * @return 结果
     */
    @Override
    public int updateAirPmlevels(AirPmlevels airPmlevels) {
        return airPmlevelsMapper.updateAirPmlevels(airPmlevels);
    }

    /**
     * 批量删除PM浓度记录
     *
     * @param recordIds 需要删除的PM浓度记录主键
     * @return 结果
     */
    @Override
    public int deleteAirPmlevelsByRecordIds(Long[] recordIds) {
        return airPmlevelsMapper.deleteAirPmlevelsByRecordIds(recordIds);
    }

    /**
     * 删除PM浓度记录信息
     *
     * @param recordId PM浓度记录主键
     * @return 结果
     */
    @Override
    public int deleteAirPmlevelsByRecordId(Long recordId) {
        return airPmlevelsMapper.deleteAirPmlevelsByRecordId(recordId);
    }

    @Override
    public void checkAndInsertWarning(AirPmlevels airPmlevels) {
        try {
            BigDecimal pm10 = airPmlevels.getPm10();
            BigDecimal pm25 = airPmlevels.getPm25();
            BigDecimal threshold1 = new BigDecimal("75.0");
            BigDecimal threshold = new BigDecimal("150.0");

            boolean pm25Exceeded = pm25.compareTo(threshold1) > 0;
            boolean pm10Exceeded = pm10.compareTo(threshold) > 0;
            // 使用compareTo方法比较大小
            if (pm25Exceeded || pm10Exceeded) {
                AirWarnings warning = new AirWarnings();
                warning.setDeviceId(airPmlevels.getDeviceId());
                warning.setWarningTime(new Date());

                if (pm25Exceeded && pm10Exceeded) {
                    warning.setWarningMessage("PM2.5与PM10均超标！当前值：PM2.5=" + pm25 + "μg/m³, PM10=" + pm10 + "μg/m³");
                } else if (pm25Exceeded) {
                    warning.setWarningMessage("PM2.5浓度超标！当前值：" + pm25 + "μg/m³");
                } else {
                    warning.setWarningMessage("PM10浓度超标！当前值：" + pm10 + "μg/m³");
                }

                airWarningsMapper.insertAirWarnings(warning);
            }
        } catch (Exception e) {
            // 异常处理（可记录日志）
            e.printStackTrace();
        }
    }
}
